#import<iostream>
#import<string>
#import<vector>

using namespace std;

#import "GGraphe.h"

template <class G, class S, class A>
GGraphe<G, S, A>::GGraphe(string nom){
	listeSommets = new vector<S *>();
	listeAretes = new vector<A *>();
	this->nom = nom;
}

template <class G, class S, class A>
GGraphe<G, S, A>::~GGraphe(){

}

template <class G, class S, class A>
void GGraphe<G, S, A>::deleteGraphe(){
	for(int i=0; i<(int)listeAretes->size();i++){
		listeAretes->at(i)->deleteArete();
	}
	for(int i=0; i<(int)listeSommets->size();i++){
		listeSommets->at(i)->deleteSommet();
	}
	//delete this;
}

//########################################################################

template <class G, class S, class A>
string GGraphe<G, S, A>::getNom(){
	return nom;
}

template <class G, class S, class A>
string GGraphe<G, S, A>::toStringComplet(){
	string s = toString()+"\n";
	for(int i = 0; i< (int)listeAretes->size(); i++){
		s += "-";
		A* a = listeAretes->at(i);
		s += a->toString() + "\n";
	}
	for(int i = 0; i< (int)listeSommets->size(); i++){
		s += "*";
		S * som = listeSommets->at(i);
		s += som->toString() + "\n";
	}
	return s;
}

//########################################################################

template <class G, class S, class A>
void GGraphe<G, S, A>::newSommet(string nom){
	listeSommets->push_back(new S(nom,this->self()));
	cout<<"New : "<<listeSommets->at(listeSommets->size()-1)->toString()<<endl;
}

template <class G, class S, class A>
void GGraphe<G, S, A>::deleteSommet(S * sommet){
	bool b=false;
	for (int i=0;i<(int)listeSommets->size(); i++){
		if (listeSommets->at(i)==sommet){
			b=true;
			break;
		}
	}
	if (b){
		sommet->deleteSommet();
		delete sommet;
	}else{
		cout<<sommet->toString()<<"n'appartient pas au graphe"<<endl;
	}
}

template <class G, class S, class A>
void GGraphe<G, S, A>::removeSommet(S * sommet){
	bool b=false;
	for (int i=0;i<(int)listeSommets->size(); i++){
		if (listeSommets->at(i)==sommet){
			b=true;
			listeSommets->erase(listeSommets->begin()+i);
			break;
		}
	}
	if (!b && sommet!=NULL) cout<<sommet->toString()<<"n'appartient pas au graphe"<<endl;
}

template <class G, class S, class A>
S * GGraphe<G, S, A>::getSommet(int i){
	if (i>=0 && i<(int)listeSommets->size())	return listeSommets->at(i);
	else return NULL;
}

template <class G, class S, class A>
int GGraphe<G, S, A>::getNumSommet(S * sommet){
	for (int i=0;i<(int)listeSommets->size(); i++){
		if (listeSommets->at(i)==sommet){
			return  i;
		}
	}
	return -1;
}

//########################################################################

template <class G, class S, class A>
void GGraphe<G, S, A>::newArete(string nom, S * sommet1, S * sommet2){
	bool b1=false;
	bool b2=false;
	for (int i=0;i<(int)listeSommets->size(); i++){
		if (listeSommets->at(i)==sommet1)b1=true;
		if (listeSommets->at(i)==sommet2)b2=true;
		if (b1 && b2) break;
	}
	if (b1 && b2){
		listeAretes->push_back(new A(nom, this->self(), sommet1, sommet2));
		cout<<"New : "<<listeAretes->at(listeAretes->size()-1)->toString()<<endl;
	}else{
		cout<<"les n'appartiennet pas au graphe"<<endl;
	}
}

template <class G, class S, class A>
void GGraphe<G, S, A>::deleteArete(A * arete){
	bool b=false;
	for (int i=0;i<(int)listeAretes->size(); i++){
		if (listeAretes->at(i)==arete){
			b=true;
			arete->deleteArete();
			delete arete;
			break;
		}
	}
	if (!b && arete!=NULL) cout<<arete->toString()<<" n'appartient pas au graphe"<<endl;
}

template <class G, class S, class A>
void GGraphe<G, S, A>::removeArete(A * arete){
	bool b=false;
	for (int i=0;i<(int)listeAretes->size(); i++){
		if (listeAretes->at(i)==arete){
			b=true;
			listeAretes->erase(listeAretes->begin()+i);
			break;
		}
	}
	if (!b && arete!=NULL) cout<<arete->toString()<<"n'appartient pas au graphe"<<endl;
}

template <class G, class S, class A>
A * GGraphe<G, S, A>::getArete(int i){
	if (i>=0 && i<(int)listeAretes->size())	return listeAretes->at(i);
	else return NULL;
}

template <class G, class S, class A>
int GGraphe<G, S, A>::getNumArete(A * arete){
	for (int i=0;i<(int)listeAretes->size(); i++){
		if (listeAretes->at(i)==arete){
			return  i;
		}
	}
	return -1;
}
